﻿package Module
{
	import Base.Debug.DebugManager;
	import Base.Loader.*;
	import Base.Log.LogManager;
	
	import Common.*;
	
	import Events.*;
	
	import Stage.*;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author fox
	 */
	public class ModuleManager
	{
		private var moduleCount:int = 0;
		
		public function ModuleManager()
		{
			
		}
		
		
		public static var instance:ModuleManager = null;
		public static function Instance():ModuleManager
		{
			if ( instance == null )
			{
				instance = new ModuleManager();
			}
			
			return instance;
		}
		
		
		public function init():void
		{
			GameLoading.instance.load();
			
			loadRes();
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.LOGINRELEASE , onLoginRelease );
		}
		
		
		public function onLoginRelease( event:ModuleEvent ):void
		{
			ModuleEventManager.instance.removeEventListener( ModuleEvent.LOGINRELEASE , onLoginRelease );
			
			GameResManager.instance.loader[ GameDefine.LOGIN_SWF ].Release();
			GameResManager.instance.loader[ GameDefine.LOGIN_SWF ] = null;
		}
		
		
		public function loadRes():void
		{
			//var xml:XML = GameResManager.instance.swf[ GameDefine.RES_SWF ].getRes().getXML( "ui" );
			//GameString.instance.Load( xml );
			
			loadModule( GameDefine.LOGIN_SWF );
			loadModule( GameDefine.MAIN_SWF );
		}
		
		
		public function loadModule( path:String ):void
		{
			// load swf
			
			moduleCount++;
			
			var swfLoader:SWFLoader = new SWFLoader();
			swfLoader.Load( path );
			swfLoader.contentLoaderInfo.addEventListener( Event.INIT , onModuleInit );
			swfLoader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS , onModuleProgress );
			
			// avoid gc callback
			GameResManager.instance.loader[ path ] = swfLoader;
		}
		
		////////////////////////////////////////////////////////
		
		private function check():void
		{
			if ( moduleCount == 0 )
			{
				// modules and res load complete
				LogManager.logOut( "modules and swf load complete" );
				
				GameStageManager.instance.init();
				
				var moduleEvent:ModuleEvent = new ModuleEvent( ModuleEvent.INIT );
				ModuleEventManager.instance.dispatchEvent( moduleEvent );
				
				GameLoading.instance.unload();
			}
		}
		
		// on module progress
		private function onModuleProgress( event:ProgressEvent ):void
		{
			GameLoading.instance.loading( "loading..." , 50 + 50 * ( 2 - moduleCount ) * ( event.bytesLoaded / event.bytesTotal )  );
		}
		
		
		// on module init 
		private function onModuleInit( event:Event ):void
		{
			// init 
			event.target.content.init();
			
			moduleCount--;
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.INIT , onModuleInit );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onModuleProgress );
			
			check();
		}
		
		
		
	}
}